home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 159 (1991-03-10)(Manewaldt, A.)(DE)(PD).zip / Taifun 159 (1991-03-10)(Manewaldt, A.)(DE)(PD).adf / SnoopDos / src / res.s < prev    next >
Text File  |  1998-03-07  |  7KB  |  137 lines

  1. *:ts=8
  2. *****************************************************************************
  3. *                                                                           *
  4. * RES.S                                    (C) Copyright Eddy Carroll 1989  *
  5. *                                                                           *
  6. * This module allows you to make a duplicate copy of your current process.  *
  7. * In fact, both processes actually share the same code. However, the        *
  8. * seglist pointer of the current process is modified so that when the       *
  9. * process terminates, the memory doesn't get freed (if it did, the new      *
  10. * process would suddenly find itself deallocated. Hello guru...)            *
  11. * This code only works when called from CLI processes.                      *
  12. *                                                                           *
  13. * The parameters passed are similar to those for CreateProc(), with the     *
  14. * difference that instead of passing a BPTR to a seglist, you pass the      *
  15. * address of the function the new process should start executing at.        *
  16. *                                                                           *
  17. * When the new process returns from this function, it will be removed from  *
  18. * the system, and its memory (finally) deallocated.                         *
  19. *                                                                           *
  20. * The typical use for a function like this is to allow a program to detach  *
  21. * itself from a CLI (completely, with no trailing console handles etc.)     *
  22. * when it is run. This is a convenient feature for the user, if the program *
  23. * is of the sort designed to sit in the background the whole time, rather   *
  24. * than do something immediately, then exit.                                 *
  25. *                                                                           *
  26. * Lattice provide cback.o which at first glance would seem to provide a     *
  27. * similar solution. However, cback.o makes it difficult to print error      *
  28. * messages to the console if there is an error on the command line - by     *
  29. * the time you spot the error, the CLI prompt has already been printed      *
  30. * and your error message is printed after it. This looks very messy.        *
  31. * Res avoids this problem by not spawning the background process until      *
  32. * after the error checking has been done.                                   *
  33. *                                                                           *
  34. * From C, you call it as follows:                                           *
  35. *                                                                           *
  36. * pid = res(name,pri,func,stacksize)                                        *
  37. *                                                                           *
  38. * name         - pointer to null terminated string                          *
  39. * pri          - integer, priority of the new process                       *
  40. * func         - pointer to the function for new process to call            *
  41. * stacksize    - integer, size of the stack for the new process             *
  42. *                                                                           *
  43. * pid          - Process ID of new process, or 0 if none created            *
  44. *                                                                           *
  45. *****************************************************************************
  46.  
  47.     INCLUDE "exec/types.i"
  48.         INCLUDE "exec/alerts.i"
  49.         INCLUDE "exec/nodes.i"
  50.         INCLUDE "exec/lists.i"
  51.         INCLUDE "exec/ports.i"
  52.         INCLUDE "exec/libraries.i"
  53.         INCLUDE "exec/tasks.i"
  54.         INCLUDE "libraries/dos.i"
  55.         INCLUDE "libraries/dosextens.i"
  56.         INCLUDE "workbench/startup.i"
  57.         INCLUDE "exec/funcdef.i"
  58.         INCLUDE "exec/exec_lib.i"
  59.         INCLUDE "libraries/dos_lib.i"
  60.  
  61.     xref    _exit
  62.     xref    _DOSBase
  63.     xdef    _res
  64.  
  65. AbsExecBase    equ    4
  66. segsize        equ    36        ; Size of fake seg. (code = 28 bytes)
  67.  
  68.         csect   text,0,0,1,2        * xref's after this are 16-bit reloc
  69.  
  70.  
  71. callsys macro
  72.         CALLLIB _LVO\1
  73.     endm
  74.  
  75. _res:
  76.     movem.l    d2-d4/a2/a3/a6,-(a7)    ; Save registers
  77.     move.l    AbsExecBase.w,a6    ; Get base of Exec library
  78.     moveq    #0,d1            ; Any sort of memory will do
  79.     moveq    #segsize,d0        ; Get size of fake segment
  80.     callsys    AllocMem        ; Grab some memory
  81.     tst.l    d0            ; Did we get any?
  82.     beq    fatal            ; If not, abort immediately!
  83.     move.l    d0,a3            ; Save pointer to memory
  84.     sub.l    a1,a1            ; NULL pointer indicates our process
  85.     callsys    FindTask        ; Get pointer to our process block
  86.     move.l    d0,a2            ; Save it
  87.     move.l    pr_CLI(A2),a0        ; Get BPTR to our process's segarray
  88.     add.l    a0,a0            ; Convert BPTR to address
  89.     add.l    a0,a0            ; 
  90.     move.l    cli_Module(a0),4(a3)    ; Make fake segment point to our code
  91.     clr.l    cli_Module(a0)        ; Remove process seg. from CLI seglist
  92.     move.l    #segsize,(a3)        ; Set size of fake seglist
  93.     lea.l    8(a3),a2        ; Get pointer to first code byte
  94.  
  95. ;
  96. ; Now a tiny machine code program is constructed. It looks like this:
  97. ;
  98. ;    move.l    #$xxxxxx,A4    ; Initialise A4
  99. ;    jsr    $xxxxxx        ; Call user program
  100. ;    move.l    #$xxxxxx,A6    ; Load DOSbase into A6
  101. ;    move.l    #$xxxxxx,d1    ; Get BPTR to this segment 
  102. ;    jmp    UnLoadSeg(A6)    ; Unload our process from memory
  103. ;
  104. ; It's built "on the fly" so to speak, to keep code size down, and also
  105. ; because it's a convenient way of initialising all the variables.
  106. ; Note that a potential problem exists if DOSBase should somehow alter or
  107. ; disappear. We'll assume it will remain relatively stable for the next
  108. ; few years anyway :-)
  109. ;
  110.  
  111.     move.l    _DOSBase,a6        ; Prepare for DOS call
  112.     move.w    #$287C,(a2)+        ; Store MOVE.L $xxxxxx,A4 instruction
  113.     move.l    a4,(a2)+        ; Output value of A4 to initialise to
  114.     move.w    #$4EB9,(a2)+        ; Store JSR $xxxxxx
  115.     move.l    36(a7),(a2)+        ; followed by address of user function
  116.     move.w    #$2C7C,(a2)+        ; Store MOVE.L $xxxxxx,A6 instruction
  117.     move.l    a6,(a2)+        ; followeds by current DOSbase
  118. ;
  119.     lea    4(a3),a3        ; Now get seglist ptr to fake segment
  120.     move.l    a3,d3            ; and convert it to BPTR
  121.     lsr.l    #2,d3            ; D3 now has seglist ptr to fake seg
  122.     move.w    #$223C,(a2)+        ; Store MOVE.L $xxxxxx,D1 instruction
  123.     move.l    d3,(a2)+        ; Followed by BPTR to the segment
  124.     move.l    #$4EEEFF64,(a2)+    ; Store JMP UnLoadSeg(A6)
  125. ;
  126.     move.l    28(A7),d1        ; Get pointer to name
  127.     move.l    32(A7),d2        ; Get process priority
  128.     move.l    40(A7),d4        ; Get stacksize
  129.     callsys    CreateProc        ; Create new process
  130.     movem.l    (a7)+,d2-d4/a2/a3/a6    ; Pop registers
  131.     rts                ; Return
  132. fatal:
  133.     moveq    #120,d0            ; Set error exit code
  134.     jmp    _exit            ; And exit
  135.  
  136.     end
  137.